﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Interface.CRM;
using NetDon.OA.Interface.Common;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Admin
{
    public partial class GoodsTravelDaysEdit :  PageBase
    {
        #region Properties
        protected Guid Id
        {
            get
            {
                return Parameters.GetGuid("Id");
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnReturn;


                if (Id != Guid.Empty)
                {
                    var service = ServiceContainer.GetService<IGoodsTravelDaysConfigService>();
                    var entity = service.GetGoodsTravelDaysConfigById(Id);
                    panelCustomInfo.DataSource = entity;
                    panelCustomInfo.DataBind();
                    ddlRouteTypeId.Enabled = true;
                    ddlReceiptOnWayDays.Enabled = true;
                    this.ucProvinceCity.EnableCompanyId = this.ucProvinceCity.EnableDepartmentId = false;
                    this.ddlRouteTypeId.SelectedValue = entity.RouteTypeId.ToString();
                    this.ucProvinceCity.ProvinceCode = entity.DeliveryProvince;
                    this.ucProvinceCity.CityCode = entity.DeliveryCity;

                    ddlGoodsTravelDaysUnit.SelectedValue = (entity.GoodsTravelDaysUnit ?? 0).ToString();
                    ddlReceiptOnWayDays.SelectedValue = (entity.ReceiptOnWayDaysUnit ?? 0).ToString();
                    

                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);



        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
    
            var customerId = Id;
          

            var service = ServiceContainer.GetService<IGoodsTravelDaysConfigService>();


            bool existName = false;
            existName = service.CheckGoodsTravelDaysConfigExists(Id, ddlRouteTypeId.SelectedValue.ToGuid(),ucProvinceCity.CityCode, CurrentApplicationContext.CompanyId);

            if (existName)
            {
                Alert("此在途信息已存在，请重新输入！");
                return;
            }
            if (Id == Guid.Empty)
            {
                customerId = Guid.NewGuid();
            }
            var data = new GoodsTravelDaysConfigData()
                           {
                               Id = customerId,

                               AreaId =CurrentApplicationContext.CompanyId,
                               RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid(),
                               DeliveryProvince = ucProvinceCity.ProvinceCode,
                               DeliveryCity =ucProvinceCity.CityCode,
                               GoodsTravelDays = int.Parse(this.txtGoodsTravelDays.Text),
                               GoodsTravelDaysUnit = int.Parse(ddlGoodsTravelDaysUnit.SelectedValue),
                               ReceiptOnWayDays = int.Parse(this.txtReceiptOnWayDays.Text),
                               ReceiptOnWayDaysUnit = int.Parse(ddlReceiptOnWayDays.SelectedValue),
                               UnitCost = this.txtUnitCost.Text.ToDecimal(),
                               RevenueUnitCost =txtRevenueUnitCost.Text.ToDecimal()


                           };
         
            bool result = Id == Guid.Empty ? service.AddGoodsTravelDaysConfig(data) : service.ModifyGoodsTravelDaysConfig(data);
            if (result)
            {
                Success("保存成功!");
                CloseWindow(true);
            }
            else
            {
                ShowDataError(data);
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {

            this.CloseWindow(false);

        }

    }

}